import MarkdownIt from 'markdown-it';

function extractHeadersPlugin(md: MarkdownIt) {
    md.core.ruler.push('extract_headers', state => {
        const tokens = state.tokens;
        const headers = [];

        for (let i = 0; i < tokens.length; i++) {
            if (tokens[i].type === 'heading_open') {
                const level = parseInt(tokens[i].tag.substr(1), 10);
                const content = tokens[i + 1].content;
                headers.push({level, content});
            }
        }
        console.log("state", state)
        // 将 headers 存储到环境变量中，以便在渲染时使用
        state.env.headers = headers;
    });
}

export default extractHeadersPlugin;
